<?php
class AdministrationController extends Zend_Controller_Action
{
	public function autentificationAction()
	{
		$form = new Form_LogAdmin();
		$_SESSION['admin']=false;
		if(!isset($_POST['login']) || !isset($_POST['mdp']))
		{
		$_POST['login']='';
		$_POST['mdp']='';
		}
		
		$this -> view -> form = $form;
		
		$parametres = new Table_Parametres();
		
		$login = ($parametres->getLogin());
		$mdp = ($parametres->getPasswd());
		
		$resu = $this-> getRequest()->getPost();			
		
		if($resu['login']==$login->LOGIN && hash('sha256',$resu['mdp'])== $mdp->PASSWD)
			{
				$_SESSION['admin']=true;
				$_SESSION['login']=$resu['login'];
												
				sleep(2);
				$this -> redirect ('/');
			}
		
						
	}
	
	public function ajoutervipAction() 
    {    
 
        $form = new Form_Ajoutervip();
    	
    	$tableV=new Table_Vips();
		$tableN=new Table_Nationalites();
    	$resuV=$tableV->getAllVip();
		$resuN=$tableN->getAllNationalites();
    	
    	$form->remplirVip($resuV);
		$form->remplirNation($resuN);
    	
    	$this -> view -> form = $form;
    	$this -> view -> form = $form;
    		
        $valeurs = $this -> getRequest() -> getPost();

		
		if ($this->getRequest()->isPost()){
			if($form->isValid($this->getRequest()->getPost())){
				$valeurs = $form->getValues();
				$nation = $valeurs["natVip"];
				
				if(empty($valeurs["sexVip"])) $sexe = NULL;
				else $sexe = $valeurs["sexVip"];

				if (empty($valeurs["dateVip"])) $dateNaiss = NULL;
				else {
					$date = $valeurs['dateVip'];
					$dateConvertie = explode("/",$date);
					$array =  array($dateConvertie[2],$dateConvertie[1],$dateConvertie[0]);
					$dateF = implode("-",$array);
					$dateNaiss = $dateF;
				}
				
				if (empty($valeurs["comVip"])) $comVip = NULL;
				else $comVip = $valeurs["comVip"];
				
				$result = $tableV -> ajouterVip($nation, $valeurs["nomVip"], $valeurs["prenomVip"], $sexe, $dateNaiss, $comVip);

				$numVip = $tableV -> getNumVipByName($valeurs["nomVip"], $valeurs["prenomVip"]);
				echo $numVip->VIP_NUMERO;
				if(!empty($valeurs["profVip"])){
					foreach($valeurs["profVip"] as $prof) {
						switch($prof){
							case 'acteur':
								$tableActeur = new Table_Acteurs();
								$resuProf = $tableActeur->ajouterActeur($numVip->VIP_NUMERO, NULL);
								break;
						
							case 'realisateur':
								$tableRealisateur = new Table_Realisateurs();
								$resuProf = $tableRealisateur->ajouterRealisateur($numVip->VIP_NUMERO);
								break;
						
							case 'chanteur':
								$tableChanteur = new Table_Chanteurs();
								$resuProf = $tableChanteur->ajouterChanteur($numVip->VIP_NUMERO, NULL);
								break;
						
							case 'mannequin':
								$tableMannequin = new Table_Mannequins();
								$resuProf = $tableMannequin->ajouterMannequin($numVip->VIP_NUMERO, NULL);
								break;
						
							case 'couturier':
								$tableCouturier = new Table_Couturiers();
								$resuProf = $tableCouturier->ajouterCouturier($numVip->VIP_NUMERO);
								break;
						}
					}
				}
				
				if(!empty($valeurs["vipmariVip"])){
					$tableMariage = new Table_Mariages();
					
					$date = $valeurs['datemariVip'];
					$dateConvertie = explode("/",$date);
					$array =  array($dateConvertie[2],$dateConvertie[1],$dateConvertie[0]);
					$dateF = implode("-",$array);
					$dateMariage = $dateF;
					
					if(!empty($valeurs["datefinmariVip"])){
						$date = $valeurs['datefinmariVip'];
						$dateConvertie = explode("/",$date);
						$array =  array($dateConvertie[2],$dateConvertie[1],$dateConvertie[0]);
						$dateF = implode("-",$array);
						$dateFinMariage = $dateF;
					
						$resuMar = $tableMariage->ajouterMariage($numVip->VIP_NUMERO, 
									$valeurs["vipmariVip"], 
									$dateMariage, 
									$dateFinMariage, 
									null);
					} else {
					
						$resuMar = $tableMariage->ajouterMariage($numVip->VIP_NUMERO,
						 $valeurs["vipmariVip"],
						 $dateMariage,
						 null, 
						 null);
					}
				}
				
				if(!empty($valeurs["vipliaiVip"])){					
					$date = $valeurs['dateliaiVip'];
					$dateConvertie = explode("/",$date);
					$array =  array($dateConvertie[2],$dateConvertie[1],$dateConvertie[0]);
					$dateF = implode("-",$array);
					$dateliai = $dateF;
					
					if(empty($valeurs["liaimotifVip"])) $motifliai = NULL;
					else $motifliai = $valeurs["liaimotifVip"];
					
					$tableLiaison = new Table_Liaisons();
					$resuLi = $tableLiaison->ajouterLiaison($numVip->VIP_NUMERO, $valeurs["vipliaiVip"], 
										$dateliai, $motifliai);
				}
				
				if(!empty($valeurs["imgVip"])){
					$adrPhoto = $valeurs["imgVip"];
					$tablePhotos = new Table_Photos();
					if(empty($valeurs["imgsujetVip"])) $sujetPhoto = null;
					else $sujetPhoto = $valeurs["imgsujetVip"];
					if(empty($valeurs["imgcomVip"])) $comPhoto = null;
					else $comPhoto = $valeurs["imgcomVip"];
					$resultPh = $tablePhotos->setPhotos(1, $numVip->VIP_NUMERO, $adrPhoto,$sujetPhoto,$comPhoto);
				}
			}
		}
		$this -> view -> form = $form;
    }
	public function modifiervipAction() 
{    
 
        $form = new Form_Modifiervip();
    	
    	$tableV=new Table_Vips();
    	$resuV=$tableV->getAllVip();
    	
    	$form->remplirVip($resuV);
    	
    	$this -> view -> form = $form;
    }
	public function supprimervipAction() 
    {
     
 		$form = new Form_supprimervip();
		$this -> view -> form = $form;
		
		$table = new Table_Vips();
		$resu=$table->getAllVip();
		$form->remplirListeVip($resu);
		
		$this -> view -> form = $form;
		
		$resu = $this-> getRequest()->getPost();
	
		if($resu!=null)	
		{
		$vipNum = $resu['vip'];

		$vips = new Table_Vips();
		$vips -> supprimerVip($vipNum);
		} 
		
    }
	
		public function supprimerphotoAction() {
			$form = new Form_Supprimerphoto();
			
			$tableVip = new Table_Vips();
			$resu=$tableVip->getAllVip();
			$form->remplirListeVip($resu);
			
			$tablePhotos = new Table_Photos();	
			$listeNum = $tablePhotos -> getNbPhotos();
			$form->remplirListePhotos($listeNum); 
			
			$this -> view -> form = $form;
			
			if($this-> getRequest() -> isPost()) {
				$valeurs=$this->getRequest()-> getPost();
				$table=new Table_Photos();
				$result= $table -> supprimerPhoto($valeurs["photo"], $valeurs["vip"]);				
			}
	}
		
		public function ajoutphotoAction()
	{
		$form = new Form_AjoutPhoto();
		
		$table = new Table_Vips();
		$resu=$table->getAllVip();
		$form->remplirListeVip($resu);
		
		$values = $form->getValues();
		if($this->getRequest()->isPost())
		{
			if($form->isValid($this->getRequest()->getPost())){
			$values = $form->getValues();
			
			
				$name=$values['img'];
				$commentaire=$values["commentaire"];
				$sujet=$values["sujet"];
				$numvip=$values['vip'];
				
				
				$photo=new Table_Photos();
				$allphotos=$photo->getAllphotos($numvip);
				$compteur=count($allphotos)+1;
				
				
				
				$resu=$photo->setPhotos($compteur,$numvip,$name,$sujet,$commentaire);
			}
		}
		
		
	
		$this -> view -> form = $form;
	}
		
}
?>